package com.fosdata.dpm.entity.dto;



import java.util.HashMap;

/**
 * Description: qbs
 * Created by yhsh on 2020/7/21 11:30
 * version 2.0
 * 方法说明  返回值定义
 */
public class ApiResult extends HashMap<String, Object> {

    private static final long serialVersionUID = -8713837118340960775L;

    // 成功
    private static final Integer SUCCESS = 0;
    // 异常 失败
    private static final Integer FAIL = 500;

    /**
     * 认证失败，需要返回重新认证
     */
    private static final Integer AUTH_FAILED = 401;

    public ApiResult() {
        put("code", SUCCESS);
        put("msg", "操作成功");
        put("data", "");
    }


    public static ApiResult authFailed(String msg) {
        ApiResult apiResult = new ApiResult();
        apiResult.put("code", AUTH_FAILED);
        apiResult.put("msg", msg);
        return apiResult;
    }

    public static ApiResult error(Object msg) {
        ApiResult apiResult = new ApiResult();
        apiResult.put("code", FAIL);
        apiResult.put("msg", msg);
        return apiResult;
    }

    public static ApiResult errors(Object msg, Object data) {
        ApiResult apiResult = new ApiResult();
        apiResult.put("code", FAIL);
        apiResult.put("msg", msg);
        apiResult.put("data", data);
        return apiResult;
    }

    public static ApiResult ok(Object msg) {
        ApiResult responseBo = new ApiResult();
        responseBo.put("code", SUCCESS);
        responseBo.put("msg", msg);
        return responseBo;
    }





    public static ApiResult ok(Object msg, Object data) {
        ApiResult responseBo = new ApiResult();
        responseBo.put("code", SUCCESS);
        responseBo.put("msg", msg);
        responseBo.put("data", data);
        return responseBo;
    }

    public static ApiResult ok() {
        return new ApiResult();
    }

    public static ApiResult error() {
        return ApiResult.error("");
    }

    public Boolean isSuccess() {
        return this.get("code").equals(SUCCESS);
    }

    public String getMsg() {
        return String.valueOf(this.get("msg"));
    }

    @Override
    public ApiResult put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
